package notif.settings;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import notif.settings.dao.Dao;

@SuppressWarnings("serial")
public class ServletCreateSetting extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    System.out.println("Creating new setting ");
    User user = (User) req.getAttribute("user");
    if (user == null) {
      UserService userService = UserServiceFactory.getUserService();
      user = userService.getCurrentUser();
    }

    String jobtitle = checkNull(req.getParameter("jobtitle"));
    String companies = checkNull(req.getParameter("companies"));
    String zipcode = checkNull(req.getParameter("zipcode"));
    String deviceid = checkNull(req.getParameter("deviceid"));

    Dao.INSTANCE.add(user.getUserId(), jobtitle, companies, zipcode, deviceid);

    resp.sendRedirect("/SettingApplication.jsp");
  }

  private String checkNull(String s) {
    if (s == null) {
      return "";
    }
    return s;
  }
} 
